'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 首测送礼
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var dataApi = require('../../../libs/dataApi');
var configUtil = require('../../../util/configUtil');
var PlayerManager = require('../../../libs/playerManager');
var activityConf = require('../../../config/activityConf');
var rewardService = require('../../../services/rewardService');
var constantUtil = require('../../../util/constantUtil');
var activityService = require('../../../services/activityService');
var actType = require('../../../consts/activityType.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 查询“首测送礼”活动列表
 */
Handler.prototype.list = function (msg, session, next) {
    var role_id = session.uid;

    var activity_list = [];
    var current_day = 1;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['activity', 'role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var activity_model = player_manager.activity;

                var open_time = moment.unix(configUtil.load('open').open_time);
                current_day = moment().startOf('day').diff(moment(open_time).startOf('day'), 'd') + 1;
                var total_days = dataApi.open_service.length();

                // 获取活动列表
                for (var i = 1; i <= total_days; i++) {
                    if (activity_model.openServerRewardIsCanReceive(i)) {
                        activity_list.push({
                            day: i,
                            is_receive: 0
                        });
                    } else {
                        activity_list.push({
                            day: i,
                            is_receive: 1
                        });
                    }
                }
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    current_day: current_day,
                    activity_list: activity_list
                }
            });
        }
    );
};

/**
 * 领取“首测送礼”奖励
 */
Handler.prototype.reward = function (msg, session, next) {
    var role_id = session.uid;
    var receive_day = msg.receive_day;

    if (!receive_day) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'activity', 'bag', 'build', 'soldier', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var activity_model = player_manager.activity;
                var role_model = player_manager.role;

                var open_time = moment.unix(configUtil.load('open').open_time);
                open_time = moment(open_time).startOf('day');
                var day = parseInt(moment().diff(moment(open_time), 'd') + 1);

                // 是否到领取日
                if (receive_day > day) {
                    return next(null, {
                        code: Code.ACTIVITY_NOT_REACH_CAN_RECEIVE_DAY
                    });
                }

                // 活动有效期
                var end_days = constantUtil.value('open_service_time');
                if (day > end_days) {
                    return next(null, {
                        code: Code.ACTIVITY_OUT_OF_DATE
                    });
                }

                // 是否重复领取
                if (!activity_model.addOpenServerReceivedDays(receive_day)) {
                    return next(null, {
                        code: Code.ACTIVITY_HAVE_RECEIVE_BEFORE
                    });
                }

                reward_list = activityConf.getOpenServiceRewardList(role_model.job_type, receive_day);
                if (!reward_list) {
                    return next(null, {
                        code: Code.REWARD_ERROR
                    });
                }
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ACT_OPEN, operationType.OPR_ACT_OPEN_REWARD);
                player_manager.save(cb);
                activityService.push(role_id, actType.OPEN, player_manager);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
